package commands;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import application.Core;

/**
 *	This class implements command cat.
 * 
 * @author Michal Mourek
 */

public class Cat extends AbstractCommand {
	
	/**
	 * Main method of this program.
	 * It concatenates file, stadard input to standard output.
	 *
	 * @param parameters List of parameters entered by user in command line.
	 * @param arguments List of arguments entered by user in command line.
	 * @param id Process ID.
	 * @param core reference to the core
	 * @author Michal Mourek
	 */
	@Override
	public void main(String[] parameters, String[] arguments, int id, Core core) {
		if (parameters.length == 0){
			if (arguments.length == 0){
				while(core.canRead(id)){	
					String s = core.read(id);
					if(s != null){
						core.write(id,s);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					
				}
			}
			else{
				for(int i = 0; i < arguments.length; i++) {
					File dir = new File(arguments[i]);
					if (dir.isFile()){
						try {
							BufferedReader bfr;
							bfr = new BufferedReader(new FileReader(dir));
							try {
								String str = bfr.readLine();
								while(str != null){
									core.write(id,str);
									try {
										Thread.sleep(50);
									} catch (InterruptedException e) {
									// TODO Auto-generated catch block
										e.printStackTrace();
									}
									str = bfr.readLine();
								}
							} catch (IOException e) {
								// TODO Auto-generated catch block
								core.getGui().writeConsole("IOException");
							}
						} catch (FileNotFoundException e) {
							core.getGui().writeConsole("FileNotFoundException");
						}
					} else {
						String[] arrayString = invalidArguments().split("\n");
						for (String string : arrayString) {
							core.write(id, string);
							try {
								Thread.sleep(50);
							} catch (InterruptedException e) {
								e.printStackTrace();
							}
						}
					}
				}
			}
		} else {
			for (int i = 0; i < parameters.length; i++) {
				char parameter = parameters[i].charAt(1);
				if (parameter == 'h'){
					String[] help = help().split("\n");
					for (String string : help) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				} else{
					String[] invPar = invalidParameters().split("\n");
					for (String string : invPar) {
						core.write(id, string);
						try {
							Thread.sleep(50);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
					break;
				}
			}
		}
	}


	/**
	 * Print help for this command.
	 *
	 * @author Michal Mourek
	 */
	@Override
	public String help() {
		// TODO Auto-generated method stub
		return ("NAME: cat - print input \n SYNOPSYS: cat [OPTION] \n \n Description: \n   -h      print help \n");
		
	}
	
	/**
	 * Print warning message, when parameter is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidParameters(){
		return ("Parameters is not correct \n Read help cat - h \n");
	}
	/**
	 * Print warning message, when argument is invalid.
	 *
	 * @author Michal Mourek
	 */
	public String invalidArguments(){
		return ("Arguments is not correct \n Read help cat - h \n");
	}
}